<template>
  <div class="div_padding">
    <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline" size="small">
      <el-form-item>
        <el-button icon="el-icon-plus" v-permission="'Create'" type="success" @click="onSubmit">添加同步</el-button>
        <Insert ref="insert" @reloadTable="reloadTable"></Insert>
      </el-form-item>
      <el-button style="float: right" icon="el-icon-refresh-right" circle @click="reloadTable"></el-button>
    </el-form>
    <el-table
      :data="tableData"
      size="mini"
      height="72vh"
      v-loading="loading"
      empty-text="查询结果为空!"
      element-loading-text="数据加载中..."
      element-loading-spinner="el-icon-loading"
      style="width: 100%;overflow-x: hidden;"
      row-key="id"
      border>
      <el-table-column
        prop="id"
        label="ID"
        v-if="showId"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="序号"
        align="center"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column
        prop="create_datetime"
        label="同步时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="creator_name"
        label="创建人"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="async_type"
        label="同步模块"
        align="center"
        width="150">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.async_type == 1">部门</el-tag>
          <el-tag v-if="scope.row.async_type == 2" type="success">角色</el-tag>
          <el-tag v-if="scope.row.async_type == 3" type="warning">人员</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="remark"
        label="备注"
        align="center"
        sortable
        width="150">
      </el-table-column>
      <el-table-column
        prop="create_datetime"
        label="创建时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        prop="update_datetime"
        label="更新时间"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="操作" align="center" width="300" prop="parent" fixed="right">
        <template slot-scope="scope">
            <el-button type="primary" v-permission="'Retrieve'" size="mini" @click="onDetail(scope.$index,scope.row)" icon="el-icon-coin" plain>查看执行结果</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin: 5px;">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="cur_page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </div>
  </div>
</template>
<script>
  import { getCeleryData,getCeleryResData } from '@/api/celery'
  import { successMsg } from '@/utils/tools'
  import Insert from './insert'
  export default {
    name:'CeleryData',
    components:{
      Insert
    },
    data() {
      return {
        showId:false,
        loading:true,
        tableData: [],
        total:0,
        cur_page:1,
        pageSize:10,
        formInline:{

        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getCeleryData({'page':this.cur_page,'limit':this.pageSize}).then(res=>{
          this.tableData=res.data.data
          this.total = res.data.total
          this.loading=false
        })
      },
      onSubmit(){
        this.$refs.insert.dialogFormVisible = true
      },
      onDetail(index,row){
        getCeleryResData({pk:row.id}).then(res=>{
          this.$alert('结果'+':'+res.data.data[0]['return_data']+','+'同步时间为'+':'+res.data.data[0]['create_datetime'], '同步结果', {
          confirmButtonText: '确定'
        });
        })
      },
      reloadTable(){
        this.loading=true
        this.fetchData()
      },
      filterData(){
        let params = {}
        params.page=this.cur_page
        params.limit=this.pageSize
        this.loading=true
        getCeleryData(params).then(res=>{
          this.tableData=res.data.data
          this.total = res.data.total  
          this.loading=false
        })
      },
      resetForm(formName){
        this.$refs[formName].resetFields()
        this.reloadTable()
      },
      handleSizeChange(val) {
        this.pageSize=val
        this.loading=true
        this.filterData()
      },
      handleCurrentChange(val) {
        this.cur_page=val
        this.loading=true
        this.filterData()
      },
      onSetting(row){
        this.$router.push({name:'rolePermission',params:{'title':row.name,'name':row.name,'key':row.key,'id':row.id}})
      }
    },
  }
</script>
<style lang="scss" scoped>
  .div_padding {
    padding: 10px 10px
  }
</style>
